#include "../import/head.h"
#include "get_http.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "os_init.h"

///////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_sgsj
{
    get_http::get_http()
    {
        m_pHttpMgr = 0;
    }

    get_http::~get_http()
    {
        term();
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int get_http::run_task( xos::i_msg * pMsg )
    {
        int ret = 0;

        data * p = g_pSysData->m_pPool->m_data_pool.GetItem();
        p->init();
        p->m_nMsg = MSG_HTTP;
        p->m_pMsg = pMsg;
        g_pSysData->m_pImpl->notify( p );
        p = 0;

        return ret;
    }

    int get_http::init()
    {
        int ret = 0;

        if( 0 == ret )
        {
            xos_http::create( g_pSysData->m_os.m_pXos, g_pSysData->m_os.m_pCommon, g_pSysData->m_os.m_pLog, &m_pHttpMgr );
            if( !m_pHttpMgr )
            {
                ret = 1;
            }
        }

        if( 0 == ret )
        {
            m_pHttpMgr->set_callback( g_pSysData->m_os.m_pMiscCallbackThread, this );
            m_pHttpMgr->set_work_thread( g_pSysData->m_os.m_pMiscCallbackThread );
        }

        if( 0 == ret )
        {
            g_pSysData->m_os.m_pHttpMgr = m_pHttpMgr;
        }

        return ret;
    }

    int get_http::term()
    {
        int ret = 0;

        g_pSysData->m_os.m_pHttpMgr = 0;

        if( m_pHttpMgr )
        {
            m_pHttpMgr->release();
            m_pHttpMgr = 0;
        }

        return ret;
    }

} // xos_sgsj
